var message = {
	UninputUserid : "用户名不能为空!",
	UninputPassword : "密码不能为空!",
	UninputValidate : "验证码不能为空!",
	BlackListUser : "非常抱歉，您的账户存在异常，已被限制登录!",
	
	DefaultUserid : "请输入用户ID"
};

$(document).ready(function() {
		$("#userid").focus(function() {
			if ($(this).val() == message.DefaultUserid) {
				$(this).attr("class","inputtextcolor");
				$(this).val("");
			}
		});
	
		$("#userid").blur(function() {
			if ($(this).val() == "" || $(this).val() == message.DefaultUserid) {
				$(this).val(message.DefaultUserid);
				$(this).attr("class","userid");
			}
		});
	
		// Enter建处理
		$("#userid").keypress(function(event) {
					if (event.keyCode == 13) {
						var userid = $("userid");
						if (userid.val() != message.DefaultUserid) {
							userid.attr("class", "inputtextcolor");
							$("#password").focus();
						}
					}
				});

		$("#password").keypress(function(event) {
					if (event.keyCode == 13) {
						var password = $("password");
						if (password.val() != "") {
							$("#code").focus();
						}
					}
				});

		$("#code").keypress(function(event) {
					if (event.keyCode == 13) {
						var code = $("code");
						if (code.val() != "") {
							// 直接跳转到登录的验证方法中
							return loginUser();
						}
					}
				});
		
		// 登录时JS验证
		$("#loginBtn").click(loginUser);

		// 生成验证码
		$('#kaptchaImage').click(changeCode);

		});

function loginUser() {
	var success = false;

	success = validataUserInfo();
	if (success) {
		success = login();
	}

	return success;
}

function validataUserInfo() {
	clearError();

	var isValid = true;
	var userid = $("#userid");
	var password = $("#password");
	var code = $("#code");

	if (userid.val().trim() == "" || userid.val().trim() == message.DefaultUserid) {
		userid.focus();
		var userNameError = $("#loginError");
		userNameError.text(message.UninputUserid);
		userNameError.css("visibility", "visible");
		isValid = false;
	} else if (password.val().trim() == "") {
		password.focus();
		var passwordError = $("#loginError");
		passwordError.text(message.UninputPassword);
		passwordError.css("visibility", "visible");
		isValid = false;
	} else if (code.val().trim() == "") {
		code.focus();
		var codeError = $("#loginError");
		codeError.text(message.UninputValidate);
		codeError.css("visibility", "visible");
		isValid = false;
	}

	return isValid;
}

function login() {
	clearError();

	var success = false;
	var jsonForm = $("#form").serialize();
	var loginUrl = "user/login";

	jQuery.ajax({
				type : "POST",
				url : loginUrl,
				cache : false,
				async : true,// 请求传输模式，默认情况下都是true:异步传输请求
				data : jsonForm,
				dataType : "json",
				success : function(data) {
					if (data != null && data.msg != null && data.msg != "") {
						var error = data.msg;
						var errElement = $("#loginError");
						errElement.text(error);
						errElement.css("visibility", "visible");
						success = false;
					} else {
						// 登陆成功
						// var sessionid = data.sessionid;

						// $.get("user/exchange?sessionid=" +
						// sessionid,null,null);
						// $.getJSON("user/exchange?sessionid=" +
						// sessionid,null);

						window.location = "user/exchangex";
						success = true;
					}
				},
				error : function() {
					alert("login Error");
				}
			});

	return success;
}

function clearError() {
	var tips = $(".tips");
	tips.text("");
	tips.css("visibility", "hidden");
}

function changeCode() {
	$('#kaptchaImage').hide().attr('src',
			'./kaptcha/getKaptchaImage?' + Math.floor(Math.random() * 100))
			.fadeIn();
	event.cancelBubble = true;
}
